<?php

namespace app\common\command;

use app\common\service\Sku as SkuService;
use mes\Mes;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;

/**
 * 同步MES尾货差异
 * @author huanghuixiang
 * @date: 2022/9/24
 */
class CronSyncStock extends Command
{

    protected $logName = 'CronSyncStock';
    protected $mes = '';
    protected $isPrint = 1;    // 是否打印信息到终端

    protected function configure()
    {
        $this->setName('CronSyncStock')
            ->addOption('type', 't', Option::VALUE_OPTIONAL, '分类', '')
            ->setDescription('尾货管理-同步MES尾货库存差异');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        ini_set('memory_limit', '512M');
        error_reporting(E_ALL & ~E_NOTICE);
        $type = $input->getOption('type');
        switch ($type) {
            case 'isSync':
                //定时查询-尾货库存数   * 默认1小时同步一次
                return $this->_cronSyncStock();
                break;
            case 'check':
                return $this->auto_check_1120();
                break;
            default:
                return $this->_cronSyncStock();
                break;
        }
    }

   /*
    * 同步MES 尾部库存
    */
    private function _cronSyncStock()
    {
        set_time_limit(0);
        $mesLib = new Mes();
        $skuLib = new SkuService();
        $result = $mesLib->getMesSkuStocks(1,50);
        $logtxt= 'totalpage'.$result['totalPageNo'];
        addLog($logtxt, 'getMesTails');
        for($x=1; $x<=$result['totalPageNo']; $x++){
            $logtxt=$x."--";
            echo  $logtxt;
            addLog($logtxt, 'getMesTails');
            sleep(2);
            $result = $mesLib->getMesSkuStocks($x,50);
            $tailStocks=$result['info'];
            foreach ($tailStocks as $key=>$item){
                $item['sizeInfo']=empty($item['sizeInfo']) ? '' : $item['sizeInfo'];
                $sizeList=$skuLib->getArrybyStockString($item['sizeInfo']);
                foreach ($sizeList as $size=>$num){
                    if(empty($num)) $num=0;
                    $insData=[ "sku"=>$item['skc'],"sync_status"=>0,"size_name"=>$size,"mes_count"=>$num, "mes_detail"=>$item['sizeInfo'],"update_time"=>time(), "create_time"=>time()];
                    $finishdata=db('sku_stock_mes')->where(['sku'=>$item['skc'],'size_name'=>$size])->count();
                    if($finishdata<1){
                        db('sku_stock_mes')->insert($insData);
                    }else if($finishdata==1) {
                        unset($insData['create_time']);
                        db('sku_stock_mes')->where(['sku'=>$item['skc'],'size_name'=>$size])->update($insData);
                    }
                }
            }
        }
        echo "全部已完成";
        return true;
    }


    //获取某页尾货数
    private function getmestail(){
        $page= $this->request->param('page',20);
        $mesLib = new Mes();
        $result = $mesLib->getMesSkuStocks($page,50);
        $logtxt= 'totalpage'.$result['totalPageNo']."-current page:".$page."<BR>";
        echo $logtxt;
        $tailStocks=$result['info'];
        foreach ($tailStocks as $key=>$item){
            print_r($item);
            echo "<br>";
        }
    }
}